<template>
  <div class="user-manage">
    <!-- 搜索栏 -->
    <el-card class="search-card" shadow="never">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="手机号">
          <el-input v-model="searchForm.searchValue" placeholder="请输入手机号" clearable style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" style="width: 150px">
            <el-option label="正常用户" :value="0"></el-option>
            <el-option label="已封禁用户" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card" shadow="never">
      <el-table :data="tableData" v-loading="loading" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="accountNumber" label="手机号" width="130"></el-table-column>
        <el-table-column prop="nickname" label="昵称" min-width="120"></el-table-column>
        <el-table-column prop="avatar" label="头像" width="80">
          <template #default="{ row }">
            <el-avatar :size="40" :src="row.avatar"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column prop="userStatus" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.userStatus === 0 ? 'success' : 'danger'">
              {{ row.userStatus === 0 ? '正常' : '已封禁' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="signInTime" label="注册时间" width="180"></el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button 
              v-if="row.userStatus === 0" 
              type="danger" 
              size="small" 
              @click="handleUpdateStatus(row, 1)">
              封禁
            </el-button>
            <el-button 
              v-else 
              type="success" 
              size="small" 
              @click="handleUpdateStatus(row, 0)">
              解封
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        :current-page="pagination.page"
        :page-size="pagination.nums"
        :total="pagination.total"
        :page-sizes="[8, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px; justify-content: flex-end">
      </el-pagination>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import api from '@/api'

const loading = ref(false)
const tableData = ref([])

const searchForm = reactive({
  searchValue: '',
  status: 0
})

const pagination = reactive({
  page: 1,
  nums: 8,
  total: 0
})

const loadData = () => {
  loading.value = true
  const params = {
    searchValue: searchForm.searchValue || '',
    mode: searchForm.status === 0 ? 1 : 2,
    page: pagination.page,
    nums: pagination.nums
  }
  
  api.queryUser(params).then(res => {
    loading.value = false
    if (res.status_code === 1) {
      tableData.value = res.data.records || []
      pagination.total = res.data.total || 0
    } else {
      ElMessage.error(res.msg || '获取数据失败')
    }
  }).catch(() => {
    loading.value = false
  })
}

const handleSearch = () => {
  pagination.page = 1
  loadData()
}

const handleReset = () => {
  searchForm.searchValue = ''
  searchForm.status = 0
  pagination.page = 1
  loadData()
}

const handleSizeChange = (val) => {
  pagination.nums = val
  pagination.page = 1
  loadData()
}

const handleCurrentChange = (val) => {
  pagination.page = val
  loadData()
}

const handleUpdateStatus = (row, status) => {
  const actionText = status === 1 ? '封禁' : '解封'
  ElMessageBox.confirm(`确定要${actionText}该用户吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    api.updateUserStatus({ id: row.id, status }).then(res => {
      if (res.status_code === 1) {
        ElMessage.success(`${actionText}成功`)
        loadData()
      } else {
        ElMessage.error(res.msg || `${actionText}失败`)
      }
    })
  }).catch(() => {})
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.user-manage {
  height: 100%;
}

.search-card {
  margin-bottom: 20px;
  border-radius: 8px;
}

.table-card {
  border-radius: 8px;
}
</style>
